//
// Created by linrui on 2022/3/21.
//

#ifndef CPP_READFILE_TOARRAY_PUBLIC_H
#define CPP_READFILE_TOARRAY_PUBLIC_H

#endif //CPP_READFILE_TOARRAY_PUBLIC_H

//
// Created by kaifamiao on 2022/3/19.
//
#define FUNC __func__
#include <cstdio>
#include <iostream>
#include <vector>
#include<io.h>
#include <string.h>
#include<algorithm> //for_each
#include <fstream>
using namespace std;
#include <iomanip>

using std::setw;

#ifndef READ_CSV_PUBLIC_H
#define READ_CSV_PUBLIC_H

#endif //READ_CSV_PUBLIC_H
#define BLACK "\033[0m\033[1;30m"
#define RED "\033[0m\033[1;31m"
#define GREEN "\033[0m\033[1;32m"
#define YELLOW "\033[0m\033[1;33m"
#define BLUE "\033[0m\033[1;34m"
#define PINK "\033[0m\033[1;35m"
#define CYAN "\033[0m\033[1;36m"
#include <ctime>
#define TEST "\033[0m\033[1;35m"
#define INFO "\033[0m\033[1;32m"
#define ERRORS "\033[0m\033[1;31m"
#define WARN "\033[0m\033[1;33m"
#define DEBUG "\033[0m\033[1;34m"
#define SHOW "\033[0m\033[1;36m"
#define GEN "\033[0m\033[1;37m\t"
void printl(char*) {};
template <typename T, typename... Types>
void printl(char *ch,const T& firstArg, const Types&... args)
{
    cout  <<ch <<firstArg <<"\033[0m";
    printl(ch,args...);
}

void logs(){};

int runcount=1;
template <typename T, typename... Types>
void logs(const T& firstArg, const Types&... args)
{
    cout  <<firstArg ;
    logs(args...);
}
void log(string,string){};
template <typename Ts, typename... Typess>
void log(string name,string info,const Ts& firstArg, const Typess&... args){
    cout <<"["<<setfill('0')<< setw(2) <<runcount<<"]" <<"["<< __TIME__<<"]";

    transform(name.begin(), name.end(), name.begin(), ::toupper);

    cout << info <<"["<< name <<"]"<<"\033[0m";
    logs(firstArg,args...);
    cout <<"\n";
    runcount++;
}

string strcolor(string ch,string str){
   return ch+ str+"\033[0m";
}
string strcolorA(string ch,string str){
    transform(str.begin(), str.end(), str.begin(), ::toupper);
    return ch+ str+"\033[0m";
}
//string strcolor(char* ch,int str){
//    return ch+ str +"\033[0m";
//}
//}
/*void log(const char *) {};
template <typename TT, typename... Typess>
void log(const TT& firstArg, const Typess&... args)
{
    cout <<firstArg  ;
    log(args...);
}*/
/*void printv(vector<string> v_files){

    for_each(v_files.begin(), v_files.end(), [](string val)->void {  cout << val << endl;});
    printl(YELLOW,"this vector size:",v_files.size(),"\n");

}*/
typedef int FuncType(int,int); //定义了FuncType这种函数数据类型
int exec(FuncType p,int a,int b)
{
    log(__func__,DEBUG,"function exec()");
    time_t begin,end;
    double ret;
    begin=clock();
    int r =p(a,b);
//    cout <<"return p("<<a<<","<<b<<"),result="<<r<<endl;
    end=clock();
    ret=double(end-begin)/CLOCKS_PER_SEC;
    log(FUNC,DEBUG,"runtime:",ret);
    return 0;
}
//C++ 获取文件夹下的所有文件名
/*
vector<string>  getFiles( const string& path) {
    //文件句柄
    long hFile;
    //文件信息
    struct _finddata_t fileinfo{};
    vector<string> files;
    string p;
    if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1) {
        do {
            //如果是目录,迭代之
            //如果不是,加入列表
            if ((fileinfo.attrib & _A_SUBDIR)) {
                if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
                    getFiles(p.assign(path).append("\\").append(fileinfo.name));
            } else {
                files.push_back(p.assign(path).append("\\").append(fileinfo.name));
            }
        } while (_findnext(hFile, &fileinfo) == 0);
        _findclose(hFile);
    }
    return files;
}

vector<string> getFiles(string path,string exteName)
{

    vector<string> files;
    char *to_search;        //欲查找的文件，支持通配符
    path=path+exteName;
    strcpy(to_search,path.c_str());//用到 c_str()函数
    long handle;                                                //用于查找的句柄
    struct _finddata_t fileinfo;                          //文件信息的结构体
    handle=_findfirst(to_search,&fileinfo);         //第一次查找
    if(-1==handle)
        files.push_back("NODATA");;
    ///printf("%s\n",fileinfo.name);                         //打印出找到的文件的文件名
    files.push_back(fileinfo.name);
    while(!_findnext(handle,&fileinfo))               //循环查找其他符合的文件，知道找不到其他的为止
    {
        files.push_back(fileinfo.name);
        //printf("%s\n",fileinfo.name);
    }
    _findclose(handle);                                      //别忘了关闭句柄
    return files;
}
vector<string> getAllFiles( string path)
{
    //文件句柄
    vector<string> files;
    long  hFile  =  0;
    //文件信息
    struct _finddata_t fileinfo;
    string p;
    if((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) != -1)
    {
        do
        {
            if((fileinfo.attrib & _A_SUBDIR))
            {
                if(strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0)
                {
                    files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
                    getAllFiles( p.assign(path).append("\\").append(fileinfo.name) );
                }
            }
            else
            {
                files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
            }
        }while(_findnext(hFile, &fileinfo) == 0);
        _findclose(hFile);
        return files;
    }
}*/
void printf_red(const char *s)
{
    printf("\033[0m\033[1;31m%s\033[0m", s);
}

void printf_green(const char *s)
{
    printf("\033[0m\033[1;32m%s\033[0m", s);
}

void printf_yellow(const char *s)
{
    printf("\033[0m\033[1;33m%s\033[0m", s);
}

void printf_blue(const char *s)
{
    printf("\033[0m\033[1;34m%s\033[0m", s);
}

void printf_pink(const char *s)
{
    printf("\033[0m\033[1;35m%s\033[0m", s);
}

void printf_cyan(const char *s)
{
    printf("\033[0m\033[1;36m%s\033[0m", s);
}
int sp(string oldstr,string newstr[]){
    int s=0;
    for (int i = 0; i < oldstr.length(); i++) { //将字符串分割成字符串数组
        if (oldstr[i] == '\t') {                 //以空格作为分隔符
            s++;
            continue;
        }
        newstr[s] += oldstr[i];                      //将分割好的字符串放到K数组里
    }
    return s;

}